package com.jlcloud.core.log.event;


import com.jlcloud.core.log.constant.EventConstant;
import com.jlcloud.core.log.feign.ILogClient;
import com.jlcloud.core.log.model.LogError;
import com.jlcloud.core.log.utils.LogAbstractUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.jlcloud.core.launch.props.JlCloudProperties;
import com.jlcloud.core.launch.server.ServerInfo;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import java.util.Map;

/**
 * 异步监听错误日志事件
 *
 * @author By: JlCloud <br>
 * @Package: com.jlcloud <br>
 * @CreateTime: 2024-12-03 21:45 <br>
 * @Copyright: 2021 www.jilianjituan.com Inc. All rights reserved. <br>
 * @Caution 注意：本内容仅限于冀联人力集团内部传阅，禁止外泄以及用于其他的商业目的 <br>
 */
@Slf4j
@AllArgsConstructor
public class ErrorLogListener {

	private final ILogClient logService;
	private final ServerInfo serverInfo;
	private final JlCloudProperties jlcloudProperties;

	@Async
	@Order
	@EventListener(ErrorLogEvent.class)
	public void saveErrorLog(ErrorLogEvent event) {
		try {
			Map<String, Object> source = (Map<String, Object>) event.getSource();
			LogError logError = (LogError) source.get(EventConstant.EVENT_LOG);
			LogAbstractUtil.addOtherInfoToLog(logError, jlcloudProperties, serverInfo);
			logService.saveErrorLog(logError);
		} catch (Exception e) {
			// 可以根据需要进行更多的异常处理，例如发送警报等
			log.error("保存错误日志时发生异常", e);
		}
	}

}
